package cn.factory;

import cn.concete.HourWorkTypeConcete;
import cn.concete.SalaryWorkTypeConcete;
import cn.concete.SaleWorkTypeConcrete;
import cn.context.StrategyWorkType;
import cn.utile.XmlParsing;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class ProductFactory {

    public StrategyWorkType createAllKindsOfProduct() {

        StrategyWorkType strategyWorkType = new StrategyWorkType();
        XmlParsing xmlParsing = new XmlParsing();
        Configuration configuration = xmlParsing.getSystemConfigXML();
        List<Object> mouth = configuration.getList("month[@value]");
        System.out.println("=========读取xml 中人员信息 =============");
        for (int i = 0; i < mouth.size(); i++) {
            System.out.println("月份==============" + mouth.get(i));
            List<Object> names = configuration.getList("month(" + i + ").employee[@name]");
            for (int k = 0; k < names.size(); k++) {
                Object name = names.get(k);
                String type = configuration.getString("month(" + i + ").employee(" + k + ")[@type]");
                String birthday = configuration.getString("month(" + i + ").employee(" + k + ")[@birthday]");
                System.out.print("员工姓名：" + name);
                System.out.print("    岗位类型：" + type);
                System.out.println("    出生日期：" + birthday);
                if(StringUtils.isBlank(type)){
                    System.out.println("    岗位类型为空：" );
                    continue;
                }
                type = type.trim();
                if ("salary".equals(type)) {
                    strategyWorkType.setWorkType(mouth.get(i).toString(), new SalaryWorkTypeConcete(birthday));
                } else if ("hour".equals(type)) {
                    Float hour = configuration.getFloat("month(" + i + ").employee(" + k + ")[@workingHours]");
                    strategyWorkType.setWorkType(mouth.get(i).toString(), new HourWorkTypeConcete(hour,birthday));
                } else if ("sale".equals(type)) {
                    String amount = configuration.getString("month(" + i + ").employee(" + k + ")[@amount]");
                    strategyWorkType.setWorkType(mouth.get(i).toString(), new SaleWorkTypeConcrete(amount,birthday));

                } else {
                    System.out.println("员工姓名："+name+"    ,无法匹配的类型" + type);
                }
            }

        }
        return strategyWorkType;
    }
}
